package com.yb.livewykt.util

import android.content.Context
import android.view.LayoutInflater
import android.widget.SeekBar
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.AppCompatSeekBar
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yb.livewykt.R
import com.yb.livewykt.bean.FilterEnum
import com.yb.livewykt.bean.FuBeautyEnum
import com.yb.livewykt.bean.MessageEvent
import com.yb.livewykt.bean.MessageEventEnum
import com.yb.livewykt.ui.adapter.LiveBeautyRecyclerAdapter
import com.yb.livewykt.ui.adapter.LiveFilterRecyclerAdapter
import org.greenrobot.eventbus.EventBus

class AlertDialogUtil {

    companion object{

        val beautySeekBar = MutableLiveData<Double>()
        val seekBarValue = MutableLiveData<Double>()

        fun showFilterDialog(context: Context):AlertDialog{
            val builder = AlertDialog.Builder(context, R.style.dialog)
            val view = LayoutInflater.from(context).inflate(R.layout.dialog_live_filter_layout,null,false)
            val seekBar = view.findViewById<AppCompatSeekBar>(R.id.beauty_strength)
            val actionRecycler = view.findViewById<RecyclerView>(R.id.action_recycler)
            actionRecycler.layoutManager = LinearLayoutManager(context).also { it.orientation = RecyclerView.HORIZONTAL }
            val actionAdapter = LiveFilterRecyclerAdapter(context)
            actionRecycler.adapter = actionAdapter
            actionAdapter.setOnItemClickListener{
                EventBus.getDefault().postSticky(MessageEvent(MessageEventEnum.FILTER,it))
            }
            seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    seekBarValue.value = progress/100.toDouble()
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {

                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {

                }
            })
            builder.setView(view)
            return builder.create()
        }

        fun showBeautyDialog(context: Context):AlertDialog{
            val builder = AlertDialog.Builder(context, R.style.dialog)
            val view = LayoutInflater.from(context).inflate(R.layout.dialog_live_filter_layout,null,false)
            val seekBar = view.findViewById<AppCompatSeekBar>(R.id.beauty_strength)
            val actionRecycler = view.findViewById<RecyclerView>(R.id.action_recycler)
            actionRecycler.layoutManager = LinearLayoutManager(context).also { it.orientation = RecyclerView.HORIZONTAL }
            val actionAdapter = LiveBeautyRecyclerAdapter(context)
            actionRecycler.adapter = actionAdapter
            actionAdapter.setOnItemClickListener{
                seekBar.progress = FuBeautyEnum.getNormalValue(it.key).times(100).toInt()
                EventBus.getDefault().postSticky(MessageEvent(MessageEventEnum.BEAUTY,it))
            }
            seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    beautySeekBar.value = progress/100.toDouble()
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {

                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {

                }
            })
            builder.setView(view)
            return builder.create()
        }



    }









}